﻿var GridCreator = function () {
    this.gridClass = "grid";
    this.rowClass = "row";
    this.cellClass = "cell";
    this.columnClass = "column";
    this.singleClass = "single";
    this.candidatesClass = "candidates";
    this.candidateClass = "candidate";


    this.createGrid = function () {
        var grid = $("<div class='" + this.gridClass + "'>");
        for (var row = 0; row < 9; row++) {
            for (var column = 0; column < 9; column++) {
                var cell = this.createCell();
                cell.addClass(this.cellClass);
                cell.addClass(this.rowClass + row);
                cell.addClass(this.columnClass + column);
                grid.append(cell);
            }
        }
        grid.append("<div style='clear:both'>");
        return grid;
    };

    this.createCell = function () {
        var cell = $("<div>");
        var cellContent = $("<div class='cell-content'>");
        cell.append(cellContent);
        var single = this.createSingle();
        var candidates = this.createCandidates();

        cellContent.append(single);
        cellContent.append(candidates);

        return cell;
    };

    this.createSingle = function () {
        var single = $("<div class='" + this.singleClass + "'>");
        var singleValue = $("<div class='value'>");
        single.append(singleValue);
        return single;
    };

    this.createCandidates = function () {
        var candidates = $("<div class='" + this.candidatesClass + "'>");
        for (var r = 0; r < 3; r++) {
            var candidateRow = $("<div class='candidateRow' >");
            for (var c = 0; c < 3; c++) {
                var k = (r * 3) + c + 1;
                var candidate = $("<div class='" + this.candidateClass + " " + this.candidateClass + k + "' >");
                var candidateValue = $("<div class='value'>");
                candidateValue.text(k);
                candidate.append(candidateValue);
                candidateRow.append(candidate);

            }
            candidates.append(candidateRow);
        }

        return candidates;
    }
}

